<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class VideoController extends AdminbaseController {
	
	function _initialize() {
		parent::_initialize();
	}
	
	function index(){
		$result = $this->curl_http(C('video_url').'v_type/getList?token='.$_SESSION["TOKEN"]);
		$types = $result['ReturnData'];
		$this->assign("types", $types);
		foreach ($types as $v){
			$typeName[$v['typeId']] = $v['name'];
		}
		$page = $this->page(100, 20);
		$status = I("post.status", -1);
		$typeId = I("post.typeId", -1);
		$name = I("post.name", "");
		$param = "beginRow=".$page->firstRow."&fetchRows=".$page->listRows."&status=".$status."&typeId=".$typeId."&name=".$name;
		$result = $this->curl_http(C('video_url').'video/getList?token='.$_SESSION["TOKEN"], $param);
		$this->assign("data", $result['ReturnData']);
		$page = $this->page($result['total'], 20);
		$this->assign("typeName", $typeName);
		$this->assign("page", $page->show('Admin'));
		$this->assign("formParam", array('status'=>$status,'name'=>$name,'typeId'=>$typeId));
		$this->display();
	}
	
	function add(){
		$result = $this->curl_http(C('video_url').'v_type/getList?token='.$_SESSION["TOKEN"]);
		$this->assign("types", $result['ReturnData']);
		$this->display();
	}
	
	function add_post(){
		if(IS_POST){
			$name = I("post.name", '');
			if(empty($name)){
				return $this->error("视频名称不能为空！");
			}
			if(empty($_FILES)){
				return $this->error("请上传视频文件！");
			}
			
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     899614161 ;// 设置附件上传大小
			$upload->exts      =     array('avi', 'mp4', 'rmvb', 'flv', 'asf', 'rm', '3gp', 'wmv', 'mpeg');// 设置附件上传类型
			$upload->rootPath  =     C('UPLOADPATH'); // 设置附件上传根目录
			$upload->savePath  =     ''; // 设置附件上传（子）目录
			
			// 上传文件
			$info = $upload->upload();
			if(!$info) {// 上传错误提示错误信息
				$this->error($upload->getError());
			}else{
				$videoName = $info['video']['name'];
				$size = $info['video']['size'];
				$ext = $info['video']['ext'];
				$sha1 = $info['video']['sha1'];
				$savename = C('UPLOADPATH').$info['video']['savepath'].$info['video']['savename'];
				$config = array('SecretId'       => C("API_SecretId"),
						'SecretKey'      => C("API_SecretKey"),
						'RequestMethod'  => 'POST',
						'DefaultRegion'  => C("API_DefaultRegion"));
				
				vendor("QcloudApi.QcloudApi");
				$service = \QcloudApi::load(\QcloudApi::MODULE_VOD, $config);
				//要输入的参数
				$package = array(
						'fileName' => $savename,                                              //文件的绝对路径，包含文件名
						'dataSize' => $size,                                          //分片大小，建议使用默认值5MB
						'isTranscode' => 1,                                                  //是否转码
						'isScreenshot' => 1,                                                 //是否截图
						'isWatermark' => 0,                                                  //是否添加水印
						'notifyUrl' =>C("notify_url")."?key=13232"              //转码完成后的回调地址，不转码此项无效
				);
				$a = $service->MultipartUploadVodFile($package);
				\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
				if($a == false){
					return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
				}else{
					
					$fileId = $a['fileId'];
					$introduction = I("post.introduction", '');
					$typeId = I("post.typeId", 1);
					$category = I("post.category", 1);
					$postData = 'introduction='.$introduction.'&name='.$name.'&typeId='.$typeId.'&category='
							.$category.'&fileId='.$fileId;
					$result = $this->curl_http(C('video_url').'video/add?token='.$_SESSION["TOKEN"], $postData);
					//删除本地文件
					unlink($savename);
					if($result['Success']){
						$this->success("添加成功！", U("Admin/Video/index"));
					}else{
						return $this->error($result['Message']);
					}
				}
			}
		}
	}
	
	function edit(){
		$videoId = I("get.videoId");
		$result = $this->curl_http(C('video_url').'video/get?token='.$_SESSION["TOKEN"], 'videoId='.$videoId);
		$result['ReturnData']['videoId'] = number_format($result['ReturnData']['videoId'], 0, '', '');
		$this->assign($result['ReturnData']);
		$result = $this->curl_http(C('video_url').'v_type/getList?token='.$_SESSION["TOKEN"]);
		$this->assign("types", $result['ReturnData']);
		$this->display();
	}
	
	function edit_post(){
		$name = I("post.name", "");
		if(empty($name)){
			return $this->error("视频名称不能为空！");
		}
		$videoId = I("post.videoId", 0);
		$introduction = I("post.introduction", '');
		$typeId = I("post.typeId", 1);
		$category = I("post.category", 1);
		$postData = 'videoId='.$videoId.'&introduction='.$introduction.'&name='.$name.'&typeId='.$typeId.'&category='.$category;
	    $result = $this->curl_http(C('video_url').'video/update?token='.$_SESSION["TOKEN"], $postData);
		if($result['Success']){
			$this->success("修改成功！", U("Admin/Video/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
	
	function disable(){
		$videoId = I("get.videoId", 0);
		$status = 0;
		$postData = 'videoId='.$videoId.'&status='.$status;
		$result = $this->curl_http(C('video_url').'video/update?token='.$_SESSION["TOKEN"], $postData);
		if($result['Success']){
			$this->success("下线成功！", U("Admin/Video/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
	
	function enable(){
		$videoId = I("get.videoId", 0);
		$status = 1;
		$postData = 'videoId='.$videoId.'&status='.$status;
		$result = $this->curl_http(C('video_url').'video/update?token='.$_SESSION["TOKEN"], $postData);
		if($result['Success']){
			$this->success("启用成功！", U("Admin/Video/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
}